
package model;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.entity.*;

public class ProductImp {
    
    public boolean CreateProduct(int categoryid, int quantity, String productname, double unitcost, double totalamount, String picture, String productdetail, String prodtype)
    {
        boolean kq = true;
        
        try {
             
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("insert into product values (?,?,?,?,?,?,?,?)");
            prst.setInt(1, categoryid);
            prst.setInt(2, quantity);
            prst.setString(3, productname);
            prst.setDouble(4, unitcost);
            prst.setDouble(5, totalamount);
            prst.setString(6, picture);
            prst.setString(7, productdetail);
            prst.setString(8, prodtype);
            kq = prst.executeUpdate()>0;
        } catch (Exception ex) {
            kq = false;
        }
        return kq;
        
    }
    
   
    public Product DetailProduct(int productid)
    {
        Product prod = new Product();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product where productid =" + productid);
            while(rs.next())
            {
                prod.setProductid(rs.getInt("productid"));
                prod.setCategoryid(rs.getInt("categoryid"));
                prod.setQuantity(rs.getInt("quantity"));
                prod.setProductname(rs.getString("productname"));
                prod.setUnitcost(rs.getDouble("unitcost"));
                prod.setTotalamount(rs.getDouble("totalamount"));
                prod.setPicture(rs.getString("picture"));
                prod.setProductdetail(rs.getString("productdetail"));
                prod.setProdtype(rs.getString("prodtype"));
            }
        } catch (Exception e) {
            prod = null;
        }
        return prod;
    }
    
    
    public boolean EditProduct(int productid,int categoryid,int quantity,String productname,double unitcost,double totalamount, String picture, String productdetail,String prodtype)
    {
        boolean kq = true;
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("update product set categoryid = ?, quantity = ?, productname = ?, unitcost = ?, totalamount = ?, picture = ?, productdetail = ? prodtype = ? where productid = ?");
            prst.setInt(1, categoryid);
            prst.setInt(2, quantity);
            prst.setString(3, productname);
            prst.setDouble(4, unitcost);
            prst.setDouble(5, totalamount);
            prst.setString(6, picture);
            prst.setString(7, productdetail);
            prst.setString(8, prodtype);
            prst.setInt(9, productid);
            prst.execute();
            prst.close();
            
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    public List<Product> Products()
    {   
        List<Product> lstProd = new ArrayList<Product>();
        
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product");
            while(rs.next())
            {
                lstProd.add(new Product(rs.getInt("productid"), rs.getInt("categoryid"), rs.getInt("quantity"), rs.getString("productname"), rs.getDouble("unitcost"), rs.getDouble("totalamount"), rs.getString("picture"), rs.getString("productdetail"), rs.getString("prodtype")));
            }
        } catch (Exception e) {
            lstProd = null;
        }
        return lstProd;
    }
    
    public List<Product> listProductbycategoryid(int categoryid)
    {
        List<Product> lpro = new ArrayList<Product>();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product p where p.categoryid = "+categoryid +" order by p.productid DESC");
            while(rs.next())
            {
                lpro.add(new Product(rs.getInt("productid"), rs.getInt("categoryid"), rs.getInt("quantity"), rs.getString("productname"), rs.getDouble("unitcost"), rs.getDouble("totalamount"), rs.getString("picture"), rs.getString("productdetail"), rs.getString("prodtype")));
            }
        } catch (Exception e) {
            lpro = null;
        }
        return lpro;
    }
    public List<Product> ProductsWithCategory(int categoryid)
    {   
        List<Product> lstProd = new ArrayList<Product>();
        
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product where categoryid = " +categoryid);
            while(rs.next())
            {
                lstProd.add(new Product(rs.getInt("productid"), rs.getInt("categoryid"), rs.getInt("quantity"), rs.getString("productname"), rs.getDouble("unitcost"), rs.getDouble("totalamount"), rs.getString("picture"), rs.getString("productdetail"), rs.getString("prodtype")));
            }
        } catch (Exception e) {
            lstProd = null;
        }
        return lstProd;
    }
    public List<Product> ProductsWithBranch(int categoryid)
    {   
        List<Product> lstProd = new ArrayList<Product>();
        
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product where categoryid = " +categoryid);
            while(rs.next())
            {
                lstProd.add(new Product(rs.getInt("productid"), rs.getInt("categoryid"), rs.getInt("quantity"), rs.getString("productname"), rs.getDouble("unitcost"), rs.getDouble("totalamount"), rs.getString("picture"), rs.getString("productdetail"), rs.getString("prodtype")));
            }
        } catch (Exception e) {
            lstProd = null;
        }
        return lstProd;
    }
    public List<Product> listProductshoppingcart(int productid, int qty)
    {
        List<Product> lpro = new ArrayList<Product>();
        lpro.add(proadd(productid, qty));
        return lpro;
    }
    
    public Product proadd(int productid, int qty)
    {
        Product pro = new Product();
         try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product p where p.productid = "+productid);
            while(rs.next())
            {
                pro.setProductid(rs.getInt("productid"));
                pro.setCategoryid(rs.getInt("categoryid"));
                pro.setQuantity(qty);
                pro.setProductname(rs.getString("productname"));
                pro.setUnitcost(rs.getDouble("unitcost"));
                pro.setTotalamount(rs.getDouble("totalamount"));
                pro.setPicture(rs.getString("picture"));
                pro.setProductdetail(rs.getString("productdetail"));
                pro.setProdtype(rs.getString("prodtype"));
                
            }
        } catch (Exception e) {
            pro = null;
        }
        return pro;
    }
    
    
    public List<Product> listProductcompare(int productid)
    {
        List<Product> lpro = new ArrayList<Product>();
        lpro.add(proaddCompare(productid));
        return lpro;
    }
    
    public Product proaddCompare(int productid)
    {
        Product pro = new Product();
         try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from product p where p.productid = "+productid);
            while(rs.next())
            {
                pro.setProductid(rs.getInt("productid"));
                pro.setCategoryid(rs.getInt("categoryid"));
                pro.setQuantity(rs.getInt("quantity"));
                pro.setProductname(rs.getString("productname"));
                pro.setUnitcost(rs.getDouble("unitcost"));
                pro.setTotalamount(rs.getDouble("totalamount"));
                pro.setPicture(rs.getString("picture"));
                pro.setProductdetail(rs.getString("productdetail"));
                pro.setProdtype(rs.getString("prodtype"));
                
            }
        } catch (Exception e) {
            pro = null;
        }
        return pro;
    }
    
    public List<Product> listpronew()
    {
        
        List<Product> lpro = new ArrayList<Product>();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("SELECT top 9 * FROM product p where p.prodtype = 'new' ORDER BY p.productid DESC");
            while(rs.next())
            {
                Product pro = new Product();
                pro.setProductid(rs.getInt("productid"));
                pro.setCategoryid(rs.getInt("categoryid"));
                pro.setQuantity(rs.getInt("quantity"));
                pro.setProductname(rs.getString("productname"));
                pro.setUnitcost(rs.getDouble("unitcost"));
                pro.setTotalamount(rs.getDouble("totalamount"));
                pro.setPicture(rs.getString("picture"));
                pro.setProductdetail(rs.getString("productdetail"));
                pro.setProdtype(rs.getString("prodtype"));
                lpro.add(pro);
                        
            }
        } catch (Exception e) {
            lpro = null;
        }
        return lpro;
    }
    public List<Product> listprobyprotype()
    {
        List<Product> lpro = new ArrayList<Product>();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("SELECT top 8 * FROM product p where p.prodtype = 'hot' ORDER BY p.productid DESC");
            while(rs.next())
            {
                Product pro = new Product();
                pro.setProductid(rs.getInt("productid"));
                pro.setCategoryid(rs.getInt("categoryid"));
                pro.setQuantity(rs.getInt("quantity"));
                pro.setProductname(rs.getString("productname"));
                pro.setUnitcost(rs.getDouble("unitcost"));
                pro.setTotalamount(rs.getDouble("totalamount"));
                pro.setPicture(rs.getString("picture"));
                pro.setProductdetail(rs.getString("productdetail"));
                pro.setProdtype(rs.getString("prodtype"));
                lpro.add(pro);
                        
            }
        } catch (Exception e) {
            lpro = null;
        }
        return lpro;
    }
    
    
    public boolean DeleteProduct(int productid)
    {
        boolean kq = true;
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("delete from product where productid = ?");
            prst.setInt(1, productid);
           
            prst.execute();
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    
    
    
}
